package com.jn.rxjava.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;

import com.jn.rx_java_demo.R;
import com.qweather.sdk.bean.weather.WeatherHourlyBean.HourlyBean;

public class HourlyWeatherAdapter extends RecyclerView.Adapter<HourlyWeatherAdapter.HourlyViewHolder> {

    private List<HourlyBean> hourlyDataList;
    private Context context;

    public HourlyWeatherAdapter(List<HourlyBean> hourlyDataList, Context context) {
        this.hourlyDataList = hourlyDataList;
        this.context = context;
    }

    @NonNull
    @Override
    public HourlyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.activiy_hourly_item, parent, false);
        return new HourlyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull HourlyViewHolder holder, int position) {
        HourlyBean hourlyData = hourlyDataList.get(position);

        // 绑定数据到视图
        holder.tempTextView.setText(hourlyData.getTemp()+"℃");
        holder.timeTextView.setText(hourlyData.getFxTime().substring(11, 16)); // 显示时间，例如 "18:00"
        holder.iconImageView.setImageResource(getIconResource(hourlyData.getIcon()));
    }

    @Override
    public int getItemCount() {
        return hourlyDataList.size();
    }

    private int getIconResource(String iconId) {
        // 这里应该根据iconId返回对应的资源ID
        // 示例映射表
        switch (iconId) {
            case "100":
                return R.drawable.ic_clear_day;
            case "101":
                return R.drawable.tiqnqi_qingtianzhuanyun;
            // 添加更多映射
            case "102":
                return R.drawable.ic_few_clouds;
            case "104":
                return R.drawable.ic_cloudy_weather;
            case "302":
                return R.drawable.ic_storm_weather;
            case "399":
                return R.drawable.rain;
            case "901":
                return R.drawable.ic_mostly_cloudy;
            default:
                return R.drawable.ic_unknown;
        }
    }

    static class HourlyViewHolder extends RecyclerView.ViewHolder {
        TextView tempTextView;
        ImageView iconImageView;
        TextView timeTextView;

        HourlyViewHolder(View itemView) {
            super(itemView);
            tempTextView = itemView.findViewById(R.id.temp001);
            iconImageView = itemView.findViewById(R.id.icon_image);
            timeTextView = itemView.findViewById(R.id.time_zhushi);
        }
    }
}